﻿package com.tudou.home.track
{
    import com.tudou.config.*;
    import com.tudou.home.model.*;
    import com.tudou.js.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.net.*;

    public class PlayerEventTracker extends Object
    {
        private var expandSended:Boolean = false;
        private var sample:TudouSample;
        private var pageInst:PageInfo;
        private static var instance:PlayerEventTracker;

        public function PlayerEventTracker()
        {
            if (PlayerEventTracker.instance != null)
            {
                throw new Error("You must use getInstance method to create PlayerEventTracker");
            }
            pageInst = PageInfo.getInstance();
            sample = UserSample.getInstance();
            return;
        }// end function

        public function sendChangeItemTrack(param1:Boolean) : void
        {
            var _loc_2:URLRequest = null;
            var _loc_3:URLVariables = null;
            _loc_2 = new URLRequest(Config.CHANGE_ITEM_URL);
            _loc_3 = new URLVariables();
            _loc_3.isMC = pageInst.changeItemManual ? (1) : (0);
            _loc_3.plyC = param1 ? (1) : (0);
            _loc_2.data = _loc_3;
            sendToURL(_loc_2);
            return;
        }// end function

        private function sendRecommendAdVideo() : void
        {
            var _loc_1:URLRequest = null;
            var _loc_2:URLVariables = null;
            var _loc_3:Array = null;
            var _loc_4:URLRequest = null;
            if (pageInst.actionID != null && pageInst.actionID != "0" && pageInst.ID != null && pageInst.ID != "0")
            {
                _loc_1 = new URLRequest(Config.AD_CPT_CLICK_URL);
                _loc_2 = new URLVariables();
                _loc_2.juid = pageInst.juid;
                _loc_2.userId = pageInst.userId;
                _loc_2.throwId = pageInst.ID;
                _loc_2.ownerid = pageInst.adOwnerId;
                _loc_2.adPositionId = pageInst.adPositionId;
                _loc_2.rand = RandString.noCache;
                _loc_1.data = _loc_2;
                sendToURL(_loc_1);
                if (pageInst.isNielson == 1)
                {
                    _loc_3 = ["ca", pageInst.adPositionId, "/ch", pageInst.channelId, "/su01/ad" + pageInst.adOwnerId, "/t3/cr" + pageInst.ID, "/b-1", "/m-1", "/do01"];
                    _loc_2 = new URLVariables();
                    _loc_2.ci = "cn-vtudou";
                    _loc_2.cg = "0";
                    _loc_2.si = _loc_3.join("");
                    _loc_2.rnd = RandString.noCache;
                    _loc_4 = new URLRequest(Config.NIELSEN_URL + "?" + unescape(_loc_2.toString()));
                    sendToURL(_loc_4);
                }
            }
            return;
        }// end function

        private function sendNielsenStat() : void
        {
            var _loc_1:* = pageInst.nielsenStat;
            if (_loc_1 && _loc_1 != "0")
            {
                sendToURL(new URLRequest(_loc_1));
            }
            return;
        }// end function

        public function sendStartEventTrack(param1:String, param2:Number) : void
        {
            send6Event(param1);
            New6Tracker.send(pageInst);
            ComScoreTracker.send(pageInst, pageInst.comScoreC6);
            sendRecommendAdVideo();
            CountTracker.send(pageInst);
            sendNielsenStat();
            return;
        }// end function

        private function send6Event(param1:String) : void
        {
            var _loc_2:Array = null;
            _loc_2 = [6, pageInst.iid, new Date().time, pageInst.sourceId, 200, pageInst.refferURL, pageInst.href, pageInst.lid, pageInst.channelId, pageInst.isRec, pageInst.juid, param1];
            var _loc_3:* = getLastPlayTime();
            if (_loc_3 != null)
            {
                _loc_2 = _loc_2.concat(_loc_3);
            }
            var _loc_4:* = pageInst.userId + "\r\n" + _loc_2.join("#") + "#end";
            var _loc_5:* = new URLRequest(Config.NUM_EVENT_URL);
            var _loc_6:* = new URLVariables();
            new URLVariables().s = _loc_4;
            _loc_6.noCache = RandString.noCache;
            _loc_5.data = _loc_6;
            sendToURL(_loc_5);
            return;
        }// end function

        private function getLastPlayTime() : Array
        {
            var lastTimeStr:String;
            var lastTimeArr:Array;
            var arr:Array;
            var lastPlayTime:Number;
            var lastPlayIID:int;
            var userID:String;
            try
            {
                lastTimeStr = JSExchanger.getInst().justNotifyJS("initPlayTimer");
                arr = lastTimeStr.split(",");
                lastTimeArr = new Array();
                if (arr != null && arr.length > 0)
                {
                    lastPlayTime = arr[0];
                    lastPlayIID = arr[1];
                    userID = arr[2];
                    if (userID != "0" && userID != null)
                    {
                        lastTimeArr.push("playtime" + userID);
                        lastTimeArr.push(lastPlayTime);
                        lastTimeArr.push(lastPlayIID);
                    }
                    pageInst.lastPlayIID = lastPlayIID;
                    pageInst.lastPlayTime = lastPlayTime / 1000;
                }
            }
            catch (e:Error)
            {
                lastTimeArr;
            }
            return lastTimeArr;
        }// end function

        public static function getInstance() : PlayerEventTracker
        {
            if (instance == null)
            {
                PlayerEventTracker.instance = new PlayerEventTracker;
            }
            return instance;
        }// end function

    }
}
